<?php defined('SYSPATH') OR die('No direct access allowed.');

class RadioButtons_Plugin extends WB_FormItem {
	
	protected $list;
	
	public function __construct($name='', $params=array())
	{
		parent::__construct($name, $params, 'radiobuttons');
		
		$this->list = isset($this->params['list']) ? $this->params['list'] : array();
	}
	
	public function prep_db()
	{
		return isset($this->list[$this->value]) ? $this->value : null;
	}

	
	/**
	 * Renders the form element
	 * 
	 * @return string
	 */
	public function render_item()
	{
		$str = $this->prefix;
		$str .= '<span class="radioset">' . $this->label . '</span>';
		foreach ($this->list as $value=>$label)
		{
			$id = $this->name.'_'.$value;
			$str .= form::label($id, $label);
			$str .= form::radio($this->name, $value, $value==$this->value, 'id="'.$id.'"');
		}
		$str .= $this->suffix;
		
		return $str;
	}
	
	/**
	 * Returns html value for a form item
	 * 
	 * @param mixed $value
	 * @return string
	 */
	public function render_cell($value)
	{
		$value = is_null($value) ? $this->value : $value;
		return (string)(isset($this->list[$value]) ? $this->list[$value] : 'not found: '.$value);
	}
}

//End of RadioButtons_Plugin Library